<template>
	<view>
		<view class="">
			<rich-text :nodes="content"></rich-text>
		</view>
	</view>
</template>

<script>
import emjoy from '@/common/js/emjoy.js';
import htmlParser from '@/common/js/html-parser';
export default {
	data() {
		return {
			emjoyList: emjoy.emjoyList,
			content:''
		};
	},
	// mixins:[emjoy],
	onLoad() {
		// emjoy.emjoyList.forEach((v)=>{
		// 	console.log(v)
		// })
		// for(let item in emjoy.emjoyList){
		// 	// console.log(emjoy.emjoyList[item])
		// 	let html="<img class='message-img' src='" + emjoy.emjoyList[item] +"'/>"
		// 	emjoy.emjoyList[item] = htmlParser(html);
		// 	console.log(emjoy.emjoyList[item])
		// 	emjoy.emjoyList[item][0].attrs.style="display: inline-block;width: 10px !important;height: 10px !important;"
		// }
		// this.emjoyList=emjoy.emjoyList;
		
		let str = '你好啊[emjoy_01]，很高兴认识你[emjoy_02]'; // 需要把[握手]和[微笑]匹配出来

		let reg = new RegExp('\\[emjoy_(.+?)\\]', 'g');

		let ss = str.replace(reg, v => {
			let string = '';
			for (let index in this.emjoyList) {
				console.log(index, this.emjoyList[index]);
				if(v==index){
					string = this.emjoyList[index]
					string = "<img class='message-img' src='" +  this.emjoyList[index] +"'/>"
					console.log(string)
					break;
				}
			}
			
			
			if(string){
				return string;
			}else{
				return v
			}
			
		});
		
		let content = htmlParser(ss);
		content.forEach(v=>{
			console.log(v)
			if(v.name=='img'){
				v.attrs.style="display: inline-block;width: 10px !important;height: 10px !important;"
			}
		})
		this.content=content;
		console.log(content);
	},
	methods: {}
};
</script>

<style scoped>
	.message-img{
		display: inline-block;
		width: 50rpx;
		height: 50rpx;
	}
	image{
		width: 50rpx;
		height: 50rpx;
	}
</style>
